package com.konsung.ksdc.manager.shop;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.OrderProductNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.widget.OnMultiClickListener;
import com.konsung.ksdc.common.widget.WaitingDialog;
import com.konsung.ksdc.config.MyApplication;
import com.konsung.ksdc.config.NetConfig;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 订单评价页面
 * Created by DLX on 2016/6/2 0002.
 */
public class OrderCommentActivity extends BaseActivity {
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tab_tv_title)
    TextView tabTvTitle;
    @Bind(R.id.tv_save)
    TextView tvSave;
    @Bind(R.id.iv_counsel)
    ImageView ivCounsel;
    @Bind(R.id.rl_title_container)
    RelativeLayout rlTitleContainer;
    @Bind(R.id.lv_buy_list)
    ListView lvBuyList;
    @Bind(R.id.rb_describe)
    RatingBar rbDescribe;
    @Bind(R.id.rb_service)
    RatingBar rbService;
    @Bind(R.id.btn_commit_comment)
    Button btnCommitComment;

    public static final String ORDER_ID = "orderId";
    public static final String BUY_LIST_DATA = "buy_list_data";

    private OrderCommentItemAdapter adapter;
    private String orderId;
    private WaitingDialog dialog;

    @Override
    protected String activityDesc() {
        return getString(R.string.page_comment);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_order_comment);
        ButterKnife.bind(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        initData();
        initView();
    }

    /**
     * 初始化评价页面数据
     */
    private void initData() {
        orderId = getIntent().getStringExtra(ORDER_ID);
    }

    /**
     * 初始化评价页面
     */
    private void initView() {
        tabTvTitle.setText(R.string.title_order_comment);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ArrayList<OrderProductNetBean> orderProductNetBeanList =
                getIntent().getParcelableArrayListExtra(BUY_LIST_DATA);
        adapter = new OrderCommentItemAdapter(this, orderProductNetBeanList);
        lvBuyList.setAdapter(adapter);

        lvBuyList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view,
                    int scrollState) {
                switch (scrollState) {
                    //当停止滚动时
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        break;

                    //滚动时
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        break;

                    //手指抬起，但是屏幕还在滚动状态
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {

            }
        });

        btnCommitComment.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                int describe = (int) rbDescribe.getRating();
                int service = (int) rbService.getRating();
                String evaluateTitle = adapter.getAllCommentStr();
                if (adapter.getInputContainer().size() == 0) {
                    Toast.makeText(OrderCommentActivity.this, getString(R.string.tip_comment_null),
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                dialog = new WaitingDialog(OrderCommentActivity.this, "");
                dialog.setText(getString(R.string.submitting));
                dialog.show();
                RequestUtils.evaluate(orderId + "", NetConfig.currentUser.getUserId() + "",
                        describe + "", service + "", evaluateTitle + "", new RequestCallback() {
                            @Override
                            public void onError(Call call, Exception e) {
                                Toast.makeText(OrderCommentActivity.this, R.string.tip_server_error,
                                        Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }

                            @Override
                            public void onResponse(ResponseBean responseBean) {
                                String resultCode = responseBean
                                        .getResultCode();
                                dialog.dismiss();
                                if (Constants.REQUEST_OK.equals(resultCode)) {
                                    Toast.makeText(MyApplication.getContext(),
                                            R.string.tip_comment_success,
                                            Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            }
                        });
            }
        });
    }
}
